package yxy.game.pm2.command.therionHunting;

import org.apache.commons.lang3.ArrayUtils;
import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TherionHuntingMessage;
import yxy.game.pm2.module.therionHunting.TherionHandle;
import yxy.game.pm2.module.therionHunting.TherionModule;
import yxy.game.pm2.module.therionHunting.TherionPlayerModule;

import java.util.Map;

/**
 * @author wrs
 * @name CMD_TherionHuntingInfo
 * @descrition 神兽猎场界面
 * @since 2022-06-10
 */
@MessageType(proto = Command.TherionHuntingInfo, classtype = TherionHuntingMessage.TherionHuntingInfoRequest.class)
public class CMD_TherionHuntingInfo extends Command<TherionHuntingMessage.TherionHuntingInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, TherionHuntingMessage.TherionHuntingInfoRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int gameType = request.getGameType(); //玩法类型；1：本服神兽争夺，2：跨服神兽猎场，3：跨服世界神兽

        TherionHuntingMessage.TherionHuntingInfoResponse.Builder response = TherionHuntingMessage.TherionHuntingInfoResponse.newBuilder();

        try {
            Map<Integer, TherionModule> moduleMap = TherionModule.getMap(gameType, playerCode);

            if (moduleMap == null) {
                send(messageSource, response, Result.ParamError);
                return;
            }

            TherionHandle handle = new TherionHandle();
            for (Integer key : moduleMap.keySet()) {
                TherionModule module = moduleMap.get(key);
                handle.mainUI(playerCode, module, gameType, key);
            }

            TherionPlayerModule playerModule = TherionPlayerModule.get(playerCode);
            if (playerModule == null) {
                send(messageSource, response, Result.ParamError);
                return;
            }

            int day = DateTime.now().getDayOfWeek();
            //封装
            for (TherionModule module : moduleMap.values()) {
                module.setOpen(ArrayUtils.contains(module.getConditions(), day));
                module.set(gameType, module.getTherionType(), playerCode);

                //是否订阅 神兽复活提示
                TherionHuntingMessage.Therion.Builder therionModuleBuilder = handle.packTherionModule(module);
                therionModuleBuilder.setIsBooked(playerModule.getIsBooked().get(gameType).get(module.getTherionType()));

                response.addTherion(therionModuleBuilder);

                response.setIsOpen(module.isOpen()); //玩法开启
            }

            send(messageSource, response, Result.Success);

        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, TherionHuntingMessage.TherionHuntingInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
